package dao

import (
	"go-admin/global"
	"go-admin/internal/model"
	mr "go-admin/internal/model/api/model_request"
	"go-admin/pkg/app"
	"time"

	_const "go-admin/internal/model/const"
)

// 创建字典
func (d *Dao) CreateDict(isSystem, _type, dictName, remark string) (string, error) {
	dict := model.Dict{
		CommonModel: model.CommonModel{
			Id:    global.GetId(),
			IsDel: _const.ISDEL,
		},
		IsSystem: isSystem,
		Type:     _type,
		DictName: dictName,
		Remark:   remark,
		IsBanned: _const.ISBANNED,
	}
	return dict.Create(d.Engine)
}

// 查询总数
func (d *Dao) CountDict(param *mr.ListDictRequest) (int, error) {
	dict := model.Dict{
		IsBanned: param.IsBanned,
		IsSystem: param.IsSystem,
		DictName: param.DictName,
	}
	return dict.Count(d.Engine)
}

// 获取字典列表
func (d *Dao) GetDictList(param *mr.ListDictRequest, pageIndex, pageSize int) ([]*model.Dict, error) {
	dict := model.Dict{
		IsBanned: param.IsBanned,
		IsSystem: param.IsSystem,
		DictName: param.DictName,
	}
	pageOffset := app.GetPageOffset(pageIndex, pageSize)
	return dict.List(d.Engine, pageOffset, pageSize)
}

// 更新字典
func (d *Dao) UpdateDict(param *mr.UpdateDictRequest) error {
	dict := model.Dict{
		CommonModel: model.CommonModel{
			Id: param.Id,
		},
	}
	values := map[string]interface{}{
		"is_banned":   param.IsBanned,
		"modified_on": time.Now().Local().Unix(),
	}
	if param.Remark != "" {
		values["remark"] = param.Remark
	}
	return dict.Update(d.Engine, values)
}

func (d *Dao) DeleteDictS(ids []string) error {
	dict := model.Dict{}
	values := map[string]interface{}{
		"deleted_on": time.Now().Local().Unix(),
		"is_del":     _const.DEL,
	}
	return dict.Deletes(d.Engine, ids, values)
}

// 通过字典名称和类型获取字典
func (d *Dao) GetDictByDictNameAndType(dictName, _type string) (*model.Dict, error) {
	dict := model.Dict{
		Type:     _type,
		DictName: dictName,
	}
	return dict.GetDictByDictNameAndType(d.Engine)
}

// 根据id获取详情
func (d *Dao) GetDictById(Id string) (*model.Dict, error) {
	dict := model.Dict{
		CommonModel: model.CommonModel{Id: Id},
	}
	return dict.GetDictById(d.Engine)
}
